from artSightWorld.apps.base.models import *
from artSightWorld.apps.map.models import *
from django.http import *

def getPhotoCoords(request, countryId, photographerId=0):
	if int(countryId) == 0 :
		if int(photographerId) == 0 :
			photos = Photo.objects.all()
		else :
			photos = Photos.objects.filter(album__photographer__id=photographerId)
	elif int(countryId) == 111111:
		photos = []
	else :
		if int(photographerId) == 0 :
			photos = Photo.objects.filter(city__region__country__id=int(countryId))
		else :
			photos = Photo.objects.filter(city__region__country__id=int(countryId)).filter(album__photographer__id=photographerId)
	content = "<liste>\n"
	citiesList = {}
	for photo in photos:
		if photo.city.pk in citiesList:
			citiesList[photo.city.pk] += 1;
		else:
			citiesList[photo.city.pk]=1;
	
	for photo in photos:
		if citiesList[photo.city.pk] == 1:
			content = content + "<photo nbPhotos=\"1\" nom=\"" + photo.title + " (par " + photo.album.photographer.username +" : "+ photo.album.nom + ")\" photographerId=\"" + str(photo.album.photographer.pk) + "\" albumId=\"" + str(photo.album.pk) + "\" latitude=\"" + str(photo.longitude) +"\" longitude=\"" + str(photo.latitude) + "\" />\n";
		elif citiesList[photo.city.pk] != 0:
			content = content + "<photo nbPhotos=\""+str(citiesList[photo.city.pk])+"\" nom=\"" + str(citiesList[photo.city.pk]) + " photos a cette coordonnee\" latitude=\"" + str(photo.longitude) +"\" longitude=\"" + str(photo.latitude) + "\" />\n";
			citiesList[photo.city.pk] = 0
	content = content + "</liste>"
	response = HttpResponse(content, mimetype="application/xml")
	return response
	
def getPhotoCoordsByAlbum(request, albumId):

	photos = Photo.objects.filter(album__id=int(albumId))
	content = "<liste>\n"
	citiesList = {}
	for photo in photos:
		if photo.city.pk in citiesList:
			citiesList[photo.city.pk] += 1;
		else:
			citiesList[photo.city.pk]=1;
	
	for photo in photos:
		if citiesList[photo.city.pk] == 1:
			content = content + "<photo nbPhotos=\"1\" nom=\"" + photo.title + " (par Nonore : "+ photo.album.nom + ")\" albumId=\"" + str(photo.album.pk) + "\" latitude=\"" + str(photo.longitude) +"\" longitude=\"" + str(photo.latitude) + "\" />\n";
		elif citiesList[photo.city.pk] != 0:
			content = content + "<photo nbPhotos=\""+str(citiesList[photo.city.pk])+"\" nom=\"" + str(citiesList[photo.city.pk]) + " photos a cette coordonnee\" albumId=\"" + str(photo.album.pk) + "\" latitude=\"" + str(photo.longitude) +"\" longitude=\"" + str(photo.latitude) + "\" />\n";
			citiesList[photo.city.pk] = 0
	content = content + "</liste>"
	response = HttpResponse(content, mimetype="application/xml")
	return response
	
def getVisitedCountries(request, photographerId):
	photos = Photo.objects.filter(album__photographer__id=int(photographerId))
	content = "<liste>\n"
	countryList = []
	for photo in photos:
		countryPhoto = photo.city.region.country
		if countryPhoto not in countryList :
			countryList.append(countryPhoto)
			content = content + "<country id=\"" + str(countryPhoto.pk) +"\"/>\n";
	content = content + "</liste>"
	response = HttpResponse(content, mimetype="application/xml")
	return response
	
def getZoomInfo(request, idAlbum):
	mapInfo = getMapInfo(int(idAlbum))
	content = "<liste>\n"
	content = content + "<zoom zoomed=\"" + str(mapInfo["zoomed"]) +"\" zoomLevel=\"" + str(mapInfo["zoomLevel"]) +"\" zoomLong=\"" + str(mapInfo["zoomLong"]) +"\" zoomLat=\"" + str(mapInfo["zoomLat"]) +"\"/>\n";
	content = content + "</liste>"
	response = HttpResponse(content, mimetype="application/xml")
	return response

def getMapInfo(idAlbum):
	mapInfo = {}
	if idAlbum == 0 :
		mapInfo["zoomed"] = 0
		mapInfo["zoomLevel"] = 0
		mapInfo["zoomLong"] = 0
		mapInfo["zoomLat"] = 0
	else :
		mapInfo["zoomed"] = 1
		photos = Photo.objects.filter(album__id=int(idAlbum))
		if len(photos)>0:
			minLat = -10000
			maxLat = -10000
			minLong = -10000
			maxLong = -10000		
			for photo in photos :
				if minLat == -10000 or minLat>photo.latitude:
					minLat = photo.latitude
				if minLong == -10000 or minLat>photo.longitude:
					minLong = photo.longitude
				if maxLat == -10000 or maxLat<photo.latitude:
					maxLat = photo.latitude
				if maxLong == -10000 or maxLong<photo.longitude:
					maxLong = photo.longitude
			mapInfo["zoomLat"] = int((minLat + (maxLat - minLat)/2) * 100000000);
			mapInfo["zoomLong"] = int((minLong + (maxLong - minLong)/2) * 100000000);
			mapInfo["zoomLevel"] = 200;
		else :
			mapInfo["zoomed"] = 0
			mapInfo["zoomLevel"] = 0
			mapInfo["zoomLong"] = 0
			mapInfo["zoomLat"] = 0
	return mapInfo